<template>
  <div class="login-container">
    <!-- 左侧装饰区域 -->
    <div class="login-left">
      <div class="left-content">
        <h1 class="system-title">员工请假管理系统</h1>
        <p class="system-subtitle">Employee Leave Management System</p>
        <div class="feature-list">
          <div class="feature-item">
            <el-icon :size="24"><CircleCheck /></el-icon>
            <span>便捷的请假申请流程</span>
          </div>
          <div class="feature-item">
            <el-icon :size="24"><CircleCheck /></el-icon>
            <span>高效的审批管理</span>
          </div>
          <div class="feature-item">
            <el-icon :size="24"><CircleCheck /></el-icon>
            <span>完善的权限控制</span>
          </div>
        </div>
        <div class="role-tips">
          <el-tag type="success" size="large" effect="plain">普通员工</el-tag>
          <el-tag type="warning" size="large" effect="plain">部门经理</el-tag>
          <el-tag type="danger" size="large" effect="plain">系统管理员</el-tag>
        </div>
      </div>
    </div>
    
    <!-- 右侧登录表单区域 -->
    <div class="login-right">
      <div class="login-form-wrapper">
        <div class="form-header">
          <h2>欢迎登录</h2>
          <p>请使用手机号验证码登录系统</p>
        </div>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          size="large"
        >
          <el-form-item prop="phone">
            <el-input
              v-model="loginForm.phone"
              placeholder="请输入手机号"
              prefix-icon="Phone"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="code">
            <div class="code-input-wrapper">
              <el-input
                v-model="loginForm.code"
                placeholder="请输入验证码"
                prefix-icon="Message"
                clearable
                maxlength="4"
              />
              <el-button
                type="primary"
                :disabled="countdown > 0"
                @click="handleSendCode"
                class="code-btn"
              >
                {{ countdown > 0 ? `${countdown}秒后重试` : '获取验证码' }}
              </el-button>
            </div>
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              class="login-btn"
              :loading="loading"
              @click="handleLogin"
            >
              立即登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { CircleCheck } from '@element-plus/icons-vue'
import { sendCode, login } from '@/api/user'
import { setToken, parseToken } from '@/utils/auth'

const router = useRouter()

// 表单数据
const loginForm = reactive({
  phone: '',
  code: ''
})

// 表单校验规则
const loginRules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { pattern: /^\d{4}$/, message: '验证码为4位数字', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)
const loading = ref(false)
const countdown = ref(0)
let timer = null

// 发送验证码
const handleSendCode = async () => {
  // 校验手机号
  if (!loginForm.phone) {
    ElMessage.warning('请先输入手机号')
    return
  }
  
  if (!/^1[3-9]\d{9}$/.test(loginForm.phone)) {
    ElMessage.warning('请输入正确的手机号')
    return
  }
  
  try {
    await sendCode(loginForm.phone)
    ElMessage.success('验证码已发送，请注意查收')
    
    // 开始倒计时
    countdown.value = 60
    timer = setInterval(() => {
      countdown.value--
      if (countdown.value <= 0) {
        clearInterval(timer)
      }
    }, 1000)
  } catch (error) {
    console.error('发送验证码失败:', error)
  }
}

// 登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  // 表单验证
  await loginFormRef.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    try {
      const res = await login(loginForm.phone, loginForm.code)
      
      // 保存 token
      const token = res.data
      setToken(token)
      
      // 解析 token 获取用户信息
      const userInfo = parseToken(token)
      if (userInfo) {
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
        console.log('用户信息:', userInfo)
      }
      
      ElMessage.success('登录成功')
      
      // 跳转到首页
      router.push('/')
    } catch (error) {
      console.error('登录失败:', error)
    } finally {
      loading.value = false
    }
  })
}

// 组件销毁时清除定时器
onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>

<style scoped>
/* 修复验证码规则描述中的错误 */
.login-container {
  display: flex;
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
  box-sizing: border-box;
}

/* 左侧装饰区域 */
.login-left {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.login-left::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -50%;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  animation: float 15s infinite;
}

@keyframes float {
  0%, 100% { transform: translate(0, 0); }
  50% { transform: translate(-20px, 20px); }
}

.left-content {
  max-width: 500px;
  color: white;
  position: relative;
  z-index: 1;
  width: 100%;
}

.system-title {
  font-size: clamp(2.5rem, 5vw, 3rem);
  font-weight: 700;
  margin: 0 0 20px 0;
  line-height: 1.2;
}

.system-subtitle {
  font-size: clamp(1.1rem, 2vw, 1.3rem);
  opacity: 0.9;
  margin: 0 0 40px 0;
  font-weight: 300;
  letter-spacing: 1px;
}

.feature-list {
  margin-bottom: 40px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 20px;
  font-size: clamp(1rem, 1.5vw, 1.2rem);
  opacity: 0.95;
}

.feature-item .el-icon {
  flex-shrink: 0;
}

.role-tips {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

/* 右侧登录表单区域 */
.login-right {
  flex: 0 0 500px;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  box-shadow: -5px 0 25px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.login-form-wrapper {
  width: 100%;
  max-width: 380px;
  box-sizing: border-box;
}

.form-header {
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: clamp(1.8rem, 3vw, 2.2rem);
  color: #333;
  margin: 0 0 12px 0;
  font-weight: 600;
}

.form-header p {
  color: #999;
  font-size: 14px;
  margin: 0;
}

.login-form {
  width: 100%;
}

.code-input-wrapper {
  display: flex;
  gap: 10px;
  width: 100%;
  align-items: stretch;
}

.code-input-wrapper :deep(.el-input) {
  flex: 1;
}

.code-btn {
  width: 120px;
  flex-shrink: 0;
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  margin-top: 10px;
}

:deep(.el-input__wrapper) {
  padding: 12px 15px;
}

:deep(.el-input__inner) {
  font-size: 15px;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

:deep(.el-button--large) {
  height: 48px;
  font-size: 15px;
}

/* 增强的响应式设计 */
@media (max-width: 1200px) {
  .login-right {
    flex: 0 0 450px;
    padding: 30px;
  }
}

@media (max-width: 992px) {
  .login-container {
    flex-direction: column;
    height: 100vh;
  }
  
  .login-left {
    display: none;
  }
  
  .login-right {
    flex: 1;
    width: 100%;
    max-width: 100%;
    padding: 20px;
    box-shadow: none;
  }
  
  .login-form-wrapper {
    max-width: 400px;
    width: 100%;
  }
}

@media (max-width: 768px) {
  .login-right {
    padding: 20px;
  }
  
  .form-header {
    margin-bottom: 30px;
  }
  
  :deep(.el-form-item) {
    margin-bottom: 20px;
  }
}

@media (max-width: 480px) {
  .login-right {
    padding: 15px;
  }
  
  .code-btn {
    width: 100px;
  }
  
  .login-form-wrapper {
    max-width: 100%;
  }
}
</style>
